import { Component, OnInit } from '@angular/core';
import * as swiper from 'swiper';
import * as $ from 'jquery';
import {GetInfoService} from '../services/get-info.service';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router';

@Component({
  selector: 'app-zhome',
  templateUrl: './zhome.component.html',
  styleUrls: ['./zhome.component.css']
})
export class ZhomeComponent implements OnInit {
  // 所有商品
  anyproduct: Array<Pro>;
  // 新鲜水果
  outfruits: Array<Pro>;
  // 蔬菜净菜
  outgreens: Array<Pro>;
  // 肉类家禽
  outmeat: Array<Pro>;
  // 海鲜水产
  outseafood: Array<Pro>;
  // 粮油副食
  outoils: Array<Pro>;
  // 零食酒水
  outsnack: Array<Pro>;
  // 蛋奶速食
  outcustard: Array<Pro>;
  // 全球代购
  outglobal: Array<Pro>;
  // 星期五
  friday: Array<Pro>;
  timeStart: any;
  timeEnd: any;
  intDay: any;
  intHour: any;
  intMinute: any;
  intSecond: any;
  constructor(private getinfo: GetInfoService, private router: Router, private http: HttpClient) {
    setInterval(() => {
      this.timeStart = new Date().getTime();
      this.timeEnd =  new Date('2018/02/10 00:00:00').getTime();
      // 计算时间差
      let timeDistance = this.timeEnd - this.timeStart;
      // 天
      this.intDay = Math.floor(timeDistance / 86400000);
      timeDistance -= this.intDay * 86400000;
      // 时
      this.intHour = Math.floor(timeDistance / 3600000);
      timeDistance -= this.intHour * 3600000;
      // 分
      this.intMinute = Math.floor(timeDistance / 60000);
      timeDistance -= this.intMinute * 60000;
      // 秒
      this.intSecond = Math.floor(timeDistance / 1000);
      // 时分秒为单数时、前面加零
      if (this.intDay < 10) {
        this.intDay = '0' + this.intDay;
      }
      if (this.intHour < 10) {
        this.intHour = '0' + this.intHour;
      }
      if (this.intMinute < 10) {
        this.intMinute = '0' + this.intMinute;
      }
      if (this.intSecond < 10) {
        this.intSecond = '0' + this.intSecond;
      }
    }, 1000);
  }

  ngOnInit() {
    // 动态获取
    this.getinfo.getInfo((data) => {
      // 全部商品
      this.anyproduct = data;
    });
    this.getinfo.getOutfruits((data) => {
      // 新鲜水果
      this.outfruits = data;
    });
    this.getinfo.getOutgreens((data) => {
      // 蔬菜净菜
      this.outgreens = data;
    });
    this.getinfo.getOutmeat((data) => {
      // 肉类家禽
      this.outmeat = data;
    });
    this.getinfo.getOutseafood((data) => {
      // 海鲜水产
      this.outseafood = data;
    });
    this.getinfo.getOutoils((data) => {
      // 粮油副食
      this.outoils = data;
    });
    this.getinfo.getOutsnack((data) => {
      // 零食酒水
      this.outsnack = data;
    });
    this.getinfo.getOutcustard((data) => {
      // 零食酒水
      this.outcustard = data;
    });
    this.getinfo.getOutglobal((data) => {
      // 零食酒水
      this.outglobal = data;
    });
    this.getinfo.getfriday((data) => {
      // 星期五
      this.friday = data;
    });
    // 轮播图
    const mySwiper = new swiper['default']('.swiper-container', {
      loop: true,

      // 如果需要分页器
      pagination: {
        el: '.swiper-pagination',
      },

      // 如果需要前进后退按钮
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      autoplay: {
        delay: 2000,
        stopOnLastSlide: false,
        disableOnInteraction: false,
      }
    });
  }
  doClick (e) {
    this.router.navigate(['/general', e.imgs, e.daclass, e.xiclass, e.depict, e.price]);
  }
}
// 自定义一个借口, 可以让外界访问
export interface Pro {
  imgs: string;
  daclass: string;
  xiclass: string;
  depict: string;
  price: number;
}
